//
//  UserModel.m
//  template
//
//  Created by SoXun on 2018/8/11.
//  Copyright © 2018年 soxun. All rights reserved.
//

#import "UserModel.h"
#import <objc/runtime.h>
//#import "BRAPPViewModel.h"
@implementation UserModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if ([key isEqualToString:@"parkInfo"]) {
        NSDictionary * dict = value;
//        self.pId = dict[@"parkId"];
//        self.parkName = dict[@"parkName"];
//        self.parkId = dict[@"parkId"];
    }
}

//- (NSString *)parkId{
//    return self.pId;
//}



- (NSDictionary *)propertiesDictionary
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    unsigned int outCount, i;
    
    //获得当前类的所有属性名，属性个数存储到outCount中
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    
    for (i = 0; i < outCount; ++i)
    {
        //获得属性名
        objc_property_t property = properties[i];
        const char *char_f = property_getName(property);
        NSString *propertyName = [NSString stringWithUTF8String:char_f];
        
        //获得属性值
        id propertyValue = [self valueForKey:propertyName];
        
        //拼接字典
        if (propertyValue)
        {
            [dict setObject:propertyValue forKey:propertyName];
        }
    }
    
    free(properties);
    
    return dict;
}


- (void)encodeWithCoder:(NSCoder *)aCoder
{
    unsigned  int count = 0;
    Ivar *ivars = class_copyIvarList(self.class, &count);
    for (int i = 0; i < count; i++) {
        const char *cname = ivar_getName(ivars[i]);
        NSString *name = [NSString stringWithUTF8String:cname];
        NSString *key = [name substringFromIndex:1];
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
    
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        
        unsigned  int count = 0;
        Ivar *ivars = class_copyIvarList(self.class, &count);
        
        for (int i = 0; i < count; i++) {
            const char *cname = ivar_getName(ivars[i]);
            NSString *name = [NSString stringWithUTF8String:cname];
            NSString *key = [name substringFromIndex:1];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
    }
    return self;
}

+ (UserModel *)getUserModel{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSData * data = [user dataForKey:@"loginUser"];
    if (data == nil) {
        return nil;
    }
    UserModel * model =   [NSKeyedUnarchiver unarchiveObjectWithData:data];
  
    return model;
}

+ (void)SaveUserModel:(UserModel *)model{
    if (!model.username || !model.tel || !model.userId || !model.token) {
        return;
    }
    NSString * type = [NSString stringWithFormat:@"%d",model.userType];
    if ([type isEqualToString:model.typeId] || [type isEqualToString:model.typeId2]) {
        
    }else{

        NSString * type2 = @"0";
        if (![model.typeId isEqualToString:@"0"]) {
            type2 = model.typeId;
        }
       
        if (![model.typeId2 isEqualToString:@"0"]) {
            type2 = model.typeId2;
        }
        model.userType = [type2 intValue];
    }
     NSData * data = [NSKeyedArchiver archivedDataWithRootObject:model];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:data forKey:@"loginUser"];
    [user synchronize];
}
+ (void)clearUserInfo{
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    NSData * data = [user dataForKey:@"loginUser"];
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loginUser"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end
